package joinpay

import (
	"encoding/json"
	"hotgo/utility/joinpay/utils"
)

const BatchPayApi = "https://www.joinpay.com/payment/pay/batchPay" // 批量代付接口

type BatchPayReq struct {
	UserNo          string `json:"userNo"`
	ProductCode     string `json:"productCode"`
	Hmac            string `json:"hmac"`
	MerchantBatchNo string `json:"merchantBatchNo"`
	RequestCount    string `json:"requestCount"`
	RequestAmount   string `json:"requestAmount"`
	RequestTime     string `json:"requestTime"`
	CallbackUrl     string `json:"callbackUrl"`
	Details         []struct {
		UserNo                string `json:"userNo"`
		MerchantOrderNo       string `json:"merchantOrderNo"`
		ReceiverAccountNoEnc  string `json:"receiverAccountNoEnc"`
		ReceiverNameEnc       string `json:"receiverNameEnc"`
		ReceiverAccountType   string `json:"receiverAccountType"`
		ReceiverBankChannelNo string `json:"receiverBankChannelNo"`
		PaidAmount            string `json:"paidAmount"`
		Currency              string `json:"currency"`
		IsChecked             string `json:"isChecked"`
		PaidDesc              string `json:"paidDesc"`
		PaidUse               string `json:"paidUse"`
	} `json:"details"`
}

type BatchPayResp struct {
	StatusCode int    `json:"statusCode"`
	Message    string `json:"message"`
	Data       struct {
		ErrorCode          string  `json:"errorCode"`
		ErrorDesc          string  `json:"errorDesc"`
		UserNo             string  `json:"userNo"`
		MerchantBatchNo    string  `json:"merchantBatchNo"`
		AcceptSuccessCount int     `json:"acceptSuccessCount"`
		AcceptFailCount    int     `json:"acceptFailCount"`
		RequestCount       int     `json:"requestCount"`
		RequestAmount      float64 `json:"requestAmount"`
		Hmac               string  `json:"hmac"`
	} `json:"data"`
}

func BatchPay(merchantKey string, req BatchPayReq) (*BatchPayResp, error) {

	marshal, err := json.Marshal(req)
	if err != nil {
		return nil, err
	}
	body, err := utils.PostRequest(BatchPayApi, marshal)
	if err != nil {
		return nil, err
	}
	var resp *BatchPayResp
	err = json.Unmarshal(body, &resp)
	return resp, nil
}
